package com.ontheroad.mysql.user.service;

import cn.modoumama.service.base.impl.BaseServiceImpl;
import com.ontheroad.core.util.MD5Encoder;
import com.ontheroad.mysql.user.mapper.UserMapper;
import com.ontheroad.user.model.UserModel;
import com.ontheroad.user.service.SystemUserService;
import net.spy.memcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by kedong on 2017/6/27 0027.
 */
@Service
@Transactional
public class SystemUserServiceImpl extends BaseServiceImpl<UserModel, Integer> implements SystemUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MemcachedClient memCachedClient;

    @Autowired
    public void setMapper(UserMapper userMapper) {
        setGenericMapper(userMapper);
    }

    @Override
    public void deleteUserByIds(String[] userIds) {
        for (String userId : userIds) {
            //删除用户
            removeById(Integer.parseInt(userId));
        }
    }

    @Override
    public int removeById(Integer id) {
        return userMapper.disabledUserById(id);
    }

    @Override
    public void updateUserPwd(UserModel record) {
        record.setPassword(MD5Encoder.encode(record.getPassword()));
        super.updateById(record);
    }

    @Override
    public List<UserModel> getOnlineUserList() {
        List<UserModel> userModels = (List<UserModel>) memCachedClient.get("onlineUsers");
//        if (userModels != null && userModels.size() > 0) {
//            return userModels;
//        }
        userModels = userMapper.getOnlineUserList();
        memCachedClient.set("onlineUsers",0, userModels);
        return userModels;
    }

    @Override
    public void updateToOfficialByUserId(Integer id) {
        userMapper.updateToOfficialByUserId(id);
    }

    @Override
    public void enableUserById(int id) {
        userMapper.enableUserById(id);
    }

}
